import 'entity/disease_entity.dart' as DiseaseBean;
import 'package:base_lib/index.dart';
import 'package:doctor_login_lib/repository/http.dart';
import 'package:rxdart/rxdart.dart';

class DiseaseBloc extends BaseBloc {
  List<String> _chipList = ["+"];

  List<String> get chipList => _chipList;

  PublishSubject<bool> loading = PublishSubject();

  getDoctorDisease(keyID) async {
    loading.add(true);
    var result = await LoginHttp.getDoctorDisease(keyID);
    if (result.isSuccess) {
      var data = DiseaseBean.DoctorDiseaseEntity.fromJson(result.data);
      _chipList.insertAll(
          _chipList.length - 1, data.data.map((e) => e.diseaseName).toList());
    } else {
      ToastUtil.error(result.error.msg);
    }
    loading.add(false);
  }

  save(context, keyID) async {
    if (_chipList.length == 1) {
      ToastUtil.error("请添加疾病标签");
      return;
    }
    List<DiseaseBean.Data> tempList =
        chipList.map((e) => DiseaseBean.Data(diseaseName: e)).toList();
    tempList.removeWhere((element) => element.diseaseName == "+");

    var result = await LoginHttp.saveDoctorDisease(keyID, tempList);
    if (result.isSuccess) {
      Routers.pop(context, tempList);
    } else {
      ToastUtil.error(result.error.msg);
    }
  }

  doDelete(String e) {
    _chipList.remove(e);
  }

  add(String e) {
    _chipList.insert(_chipList.length - 1, e);
  }

  @override
  void dispose() {
    loading?.close();
  }
}
